<template>
	<cl-dialog
		:title="type ? '分配主管' : '分配店长'"
		width="500px"
		:visible.sync="visible"
		:props="{
			appendToBody: true,
			closeOnClickModal: false,
			closeOnPressEscape: false
		}"
		@close="close"
	>
		<el-form ref="form" :model="form" :rules="rules" label-position="top" :disabled="saving">
			<el-form-item label="选择员工" prop="user_id">
				<cl-select
					v-model="form.user_id"
					placeholder="请选择员工分配"
					style="width:100%"
					filterable
				>
					<el-option
						v-for="(item, index) in userList"
						:key="index"
						:label="item.user_name"
						:value="item.user_id"
					/>
				</cl-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="close">关闭</el-button>
			<el-button type="primary" @click="submit" :loading="saving">确定</el-button>
		</template>
	</cl-dialog>
</template>
<script>
export default {
	data() {
		return {
			visible: false,
			form: {
				user_id: "",
				dept_id: ""
			},
			rules: {
				user_id: [{ required: true, message: "请选择员工分配" }]
			},
			saving: false,
			userList: [],
			type: null
		};
	},
	methods: {
		open(row = {}, type) {
			this.visible = true;
			this.form.dept_id = row.dept_id;
			this.form.user_id = row.manage_id || "";
      	this.type = type;
			this.getUserList(row.first_zone_id,row.biz_id);

		},
		close() {
			if (this.saving) return;
			this.visible = false;
			this.reset();
			this.type = null;
		},
		reset() {
			this.saving = false;
			this.userList = [];
			this.$refs.form.resetFields();
		},
		submit() {
			this.$refs.form.validate(async (valid, errors) => {
				this.showFormErr(errors);
				if (!valid) return;
				this.saving = true;
				await this.$service.dept.setDeptManager(this.form).done(() => {
					this.saving = false;
				});
				this.$message.success("操作成功");
				this.$emit("confirm");
				this.saving = false;
				this.close();
			});
		},
		// 员工列表
		async getUserList(first_zone_id,biz_id) {
			let data = [];
			if (this.type) {
				data = await this.$service.user.setDeptuserlists({post:1,biz_id}).done(() => {
					this.saving = false;
				})
			} else {
				data = await this.$service.user.getShopTeachers({});
			}

			this.userList = data;
		}
	}
};
</script>
